<?php

/**
 * 城市检测
 *
 */
class Handler_city {

    private function __construct() {
        
    }

    /**
     * 日本城市别名
     *
     * @return str
     */
    static public function alias_city($city) {
        $arr = array(
            '襄樊' => '襄阳',
                /* '青森'=>'青森县',
                  '岩手'=>'岩手县',
                  '宫城'=>'宫城县',
                  '秋田'=>'秋田县',
                  '山形'=>'山形县',
                  '福岛'=>'福岛县',
                  '东京'=>'东京都',
                  '神奈川'=>'神奈川县',
                  '崎玉'=>'崎玉县',
                  '千叶'=>'千叶县',
                  '茨城'=>'茨城县',
                  '栃木'=>'栃木县',
                  '群马'=>'群马县',
                  '山梨'=>'山梨县',
                  '新潟'=>'新潟县',
                  '长野'=>'长野县',
                  '富山'=>'富山县',
                  '石川'=>'石川县',
                  '福井'=>'福井县',
                  '爱知'=>'爱知县',
                  '岐阜'=>'岐阜县',
                  '静冈'=>'静冈县',
                  '三重'=>'三重县',
                  '大阪'=>'大阪府',
                  '兵库'=>'兵库县',
                  '京都'=>'京都府',
                  '滋贺'=>'滋贺县',
                  '奈良'=>'奈良县',
                  '和歌山'=>'和歌山县',
                  '鸟取'=>'鸟取县',
                  '岛根'=>'岛根县',
                  '冈山'=>'冈山县',
                  '广岛'=>'广岛县',
                  '山口'=>'山口县',
                  '德岛'=>'德岛县',
                  '香川'=>'香川县',
                  '爱媛'=>'爱媛县',
                  '高知'=>'高知县',
                  '福冈'=>'福冈县',
                  '佐贺'=>'佐贺县',
                  '长崎'=>'长崎县',
                  '熊本'=>'熊本县',
                  '大分'=>'大分县',
                  '宫崎'=>'宫崎县',
                  '鹿儿岛'=>'鹿儿岛县',
                  '冲绳'=>'冲绳县',
                 */
        );
        return $arr[$city];
    }

    /**
     * 获取城市信息。
     *
     * @param mixed $city
     * @return array
     */
    static public function get_citys($city) {

        if (empty($city)) {
            return array();
        }
        if (!is_string($city)) {
            $city = (string) $city;
        }
        $Cache_Lite = MemoryCache::getInstance();
        $Cache_key = md5('Handler_city_get_citys_webdev_new' . $city);
        if (!$return = $Cache_Lite->get($Cache_key)) {
            $db = DB_MySQLi::getInstance(1);
            if (is_numeric($city) && $city[0] == 0) {
                $_row = $db->getData_ASSOCS("select city_id,province_id,city_name,city_enname,std_code from cms_cities where city_code='" . $city . "'");
            } elseif (strlen($city) <= 3 && is_numeric($city)) {//数字city_id
                $_row = $db->getData_ASSOCS("select city_id,province_id,city_name,city_enname,std_code from cms_cities where city_id='" . $city . "'");
            } elseif (ord($city[0]) >= 65 && ord($city[0]) <= 90 || ord($city[0]) >= 97 && ord($city[0]) <= 122) {//字母 city_enname
                $city = strtoupper($city);
                $_row = $db->getData_ASSOCS("select city_id,province_id,city_name,city_enname,std_code from cms_cities where city_enname='" . $city . "'");
            } else {// 其它  汉字 city_name
                $_row = $db->getData_ASSOCS("select city_id,province_id,city_name,city_enname,std_code from cms_cities where city_name='" . $city . "'");
                if (empty($_row[0])) {
                    $city2 = Handler_city::alias_city($city);
                    $_row = $db->getData_ASSOCS("select city_id,province_id,city_name,city_enname,std_code from cms_cities where city_name='" . $city2 . "'");
                    $_row[0]['alias_city_name'] = $city;
                }
            }
            if (empty($_row[0])) {
                $return = array();
            } else {
                $return = $_row[0];
            }
            $return['city_enname'] = strtolower($return['city_enname']);
            $Cache_Lite->set($Cache_key, $return, 86400);
        }
        return $return;
    }

    /**
     * 取得城市名称
     *
     * @param str $cityids 逗号分隔的cityID
     * @return array | bool
     */
    public static function get_city_list($cityids = '') {
        $cityids = trim($cityids, ',');
        if (empty($cityids)) {
            return false;
        }
        $return = array();
        $db = DB_MySQLi::getInstance(1);
//        $sql = 'select city_id,city_name,city_enname from cms_cities where city_id in (' . $cityids . ')';
        $sql = "select city_id,city_name,city_enname from cms_cities where city_id in ($cityids) order by find_in_set(city_id,'$cityids')";
        $dbre = $db->query($sql);
        while ($row = $db->fetchRow_ASSOCS()) {
            $row['city_enname'] = strtolower($row['city_enname']);
            $return[$row['city_id']] = $row;
        }
        return $return;
    }

    /**
     * 通过城市名称获得城市id
     *
     * @param str $city_name
     * @return int
     */
    function getCityIdByCityName($city_name = '') {
        if (empty($city_name)) {
            return 15;
        }
        $db = DB_MySQLi::getInstance(1);
        $_row = $db->getData_ASSOCS("select city_id from cms_cities where city_name='" . $city_name . "'");
        $city_id = $_row[0]['city_id'];
        !$city_id && $city_id = 15;
        return $city_id;
    }

    /**
     * 通过城市id获得城市名称
     *
     * @param str $city_id
     * @return int
     */
    public static function getCityNameByCityId($city_id = '') {
        if (empty($city_id)) {
            return '';
        }
        $city_id = intval($city_id);
        $db = DB_MySQLi::getInstance(1);
        $_row = $db->getData_ASSOCS("select city_name from cms_cities where city_id=" . $city_id);
        return $_row[0]['city_name'];
    }

    /*
     * 按拼音搜索指定城市的酒店列表,用于酒店搜索提示;
     * 
     * $city --指定城市的ID
     * $ename --用户键入的字母
     */

    function GetCityHotelsByEname($city, $ename) {
        //$db = DB_MySQLi::getInstance(1);
        //$_sql = "select name from hotel_list where is_valid=1 and city_id=$city and (name_pinyin like '%$ename%' or name_en like '%$ename%' or name like '%$ename%') and is_valid=1 limit 0,15";
        $Cache_Lite = MemoryCache::getInstance();
        $Cache_key = md5('GetCityHotelsByEname_' . $city . '_' . $ename);
        if (!$hotels = $Cache_Lite->get($Cache_key)) {
            $db = mssql_connect::get();
            $_sql = "select top 15 HotelName as name from Hotel where isValid=1 and cityId=$city and (HotelNameAb like '%$ename%' or HotelEname like '%$ename%' or HotelName like '%$ename%')";
            $dbre = $db->query($_sql);
            $hotels = array();
            while ($h = $db->fetch_array($dbre)) {
                $hotels[] = $h;
            }
            $Cache_Lite->set($Cache_key, $hotels, hotel_service::$cache_expire['1h']);
        }
        return $hotels;
    }

    /**
     *
     * 根据城市中文名获取三字码
     * @param string $cityCname
     */
    static public function getCityCodeByCityCname($cityCname = '') {
        $cache = MemoryCache::getInstance();
        $tmpCacheInfo = $cache->get('cityCodeByCityCname' . $cityCname);
        if (!$tmpCacheInfo) {
            require_once (DATA_PATH . 'Flight_static.class.php');
            foreach (Flight_static::$AirPort as $cityCode => $cityInfo) {
                if ($cityCname == $cityInfo[0]) {
                    $tmpCacheInfo = $cityCode;
                    break;
                }
            }
            $cache->set('cityCodeByCityCname' . $cityCname, $tmpCacheInfo, 86400 * 30);
        }
        return $tmpCacheInfo;
    }

}

?>